// pages/Main/mainIndex/mainIndex.js
var app = getApp();
var common = require('../../../utils/common.js')
var businessLogic = require('../../../utils/NetWork/Model/MainbusinessLogic.js')
let animationShowHeight = 300;

Page({

  /**
   * 页面的初始数据
   */

  data: {
    animationData: "",
    showModalStatus: false,
    imageHeight: 0,
    imageWidth: 0,
    orgid: "",
    userid: "",
    examid: "",
    open: false,
    mark: 0,
    newmark: 0,
    startmark: 0,
    endmark: 0,
    windowWidth: wx.getSystemInfoSync().windowWidth,
    staus: 1,
    translate: '',
    infodata: '',
    denfenqk:'',
    peopleid:''
   
  },
  imageLoad: function (e) {
    this.setData({ imageHeight: e.detail.height, imageWidth: e.detail.width });
  },
  showModal: function () {
    // 显示遮罩层  
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(animationShowHeight).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    // 隐藏遮罩层  
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation;
    animation.translateY(animationShowHeight).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let c = common.Getorgid();
   
    this.setData({
      orgid: c.orgid,
      userid: c.userid,
      examid: options.examid,
      //extype: options.type,
      parid: options.parid,
      name:options.name,
      jlid:options.jlid,
     
    });
    this.papercontent();
    this.defen();
  },

  papercontent: function () {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "id": this.data.parid, }
    businessLogic.QueryTestC(params, (res) => {
      // console.log("8.5.2", res)
      this.setData({
        infodata: res.papercontent
      })
      //console.log(this.data.infodata)
       this.answersay();
    })
  },

 
  answersay: function () {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "peopleid,examid", "searchValue": this.data.name + "," + this.data.examid + "," }
    businessLogic.QueryTestAnswer(params, (res) => {
      common.showLoading("加载中", "loading");
      this.setData({
        listdata: res,
      })
      // console.log("8.5.3", res)
      for (var k = 0; k < this.data.listdata.length; k++) {
        for (var i = 0; i < this.data.infodata.length; i++) {
          for (var j = 0; j < this.data.infodata[i].qt.length; j++) {
            if (this.data.infodata[i].qt[j].typecode == '1ab96071-3741-40b7-983c-bba5a031fe0d') {
              for (var m = 0; m < this.data.infodata[i].qt[j].csub.length; m++) {
                if (this.data.infodata[i].qt[j].csub[m].id == this.data.listdata[k].testid) {
                  this.data.infodata[i].qt[j].csub[m].say = this.data.listdata[k].say;
                  this.data.infodata[i].qt[j].csub[m].point = this.data.listdata[k].point;
                }
              }
            }
            else {
              if (this.data.infodata[i].qt[j].id == this.data.listdata[k].testid) {
                this.data.infodata[i].qt[j].say = this.data.listdata[k].say;
                this.data.infodata[i].qt[j].point = this.data.listdata[k].point;
              }
            }
          }
        }
      }
      this.setData({
        newdata: this.data.infodata,
        peopleid: res[0].peopleid,
      })
      common.hideLoading();
      //console.log(this.data.newdata)
    })
  },

 
  defen: function () {
    let params = { "orgid": this.data.orgid, "userid": this.data.userid, "id": this.data.jlid, }
    businessLogic.TestScores(params, (res) => {
    
      res.newstime = common.toDate3(res.newstime);
      res.dotime = common.formatSeconds(res.dotime )
      this.setData({
        denfenqk: res
      })
    //  console.log("6.7.1", this.data.denfenqk)
    })
  },
  student: function () {
    wx.navigateTo({
      url: "../post/post"
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    wx.getSystemInfo({
      success: function (res) {
        animationShowHeight = res.windowHeight;
      }
    })
  },

  tap_ch: function (e) {
    if (this.data.open) {
      this.setData({
        translate: 'transform: translateX(0px)'
      })
      this.data.open = false;
    } else {
      this.setData({
        translate: 'transform: translateX(' + this.data.windowWidth * 0.75 + 'px)'
      })
      this.data.open = true;
    }
  },
  tap_start: function (e) {
    this.data.mark = this.data.newmark = e.touches[0].pageX;
    if (this.data.staus == 1) {
      // staus = 1指默认状态
      this.data.startmark = e.touches[0].pageX;
    } else {
      // staus = 2指屏幕滑动到右边的状态
      this.data.startmark = e.touches[0].pageX;
    }

  },
  tap_drag: function (e) {
    /*
     * 手指从左向右移动
     * @newmark是指移动的最新点的x轴坐标 ， @mark是指原点x轴坐标
     */
    this.data.newmark = e.touches[0].pageX;
    if (this.data.mark < this.data.newmark) {
      if (this.data.staus == 1) {
        if (this.data.windowWidth * 0.75 > Math.abs(this.data.newmark - this.data.startmark)) {
          this.setData({
            translate: 'transform: translateX(' + (this.data.newmark - this.data.startmark) + 'px)'
          })
        }
      }

    }
    /*
     * 手指从右向左移动
     * @newmark是指移动的最新点的x轴坐标 ， @mark是指原点x轴坐标
     */
    if (this.data.mark > this.data.newmark) {
      if (this.data.staus == 1 && (this.data.newmark - this.data.startmark) > 0) {
        this.setData({
          translate: 'transform: translateX(' + (this.data.newmark - this.data.startmark) + 'px)'
        })
      } else if (this.data.staus == 2 && this.data.startmark - this.data.newmark < this.data.windowWidth * 0.75) {
        this.setData({
          translate: 'transform: translateX(' + (this.data.newmark + this.data.windowWidth * 0.75 - this.data.startmark) + 'px)'
        })
      }

    }

    this.data.mark = this.data.newmark;

  },
  tap_end: function (e) {
    if (this.data.staus == 1 && this.data.startmark < this.data.newmark) {
      if (Math.abs(this.data.newmark - this.data.startmark) < (this.data.windowWidth * 0.2)) {
        this.setData({
          translate: 'transform: translateX(0px)'
        })
        this.data.staus = 1;
      } else {
        this.setData({
          translate: 'transform: translateX(' + this.data.windowWidth * 0.75 + 'px)'
        })
        this.data.staus = 2;
      }
    } else {
      if (Math.abs(this.data.newmark - this.data.startmark) < (this.data.windowWidth * 0.2)) {
        this.setData({
          translate: 'transform: translateX(' + this.data.windowWidth * 0.75 + 'px)'
        })
        this.data.staus = 2;
      } else {
        this.setData({
          translate: 'transform: translateX(0px)'
        })
        this.data.staus = 1;
      }
    }

    this.data.mark = 0;
    this.data.newmark = 0;
  },
  paperDetailon: function () {
    wx.navigateTo({
      url: "../answeron/answeron?title=班级测试统计&&remarktype=1&&examtype=1&&examid=" + this.data.examid + "&&parid=" + this.data.parid + "&&name=" + this.data.name + "&&peopleid=" + this.data.peopleid + "&&jlid=" + this.data.jlid
    });
  }

})
